var wxRequest = require('../../utils/wxRequest')
var Common = require('../../utils/common');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:0,
    familyInfo:null,
    sfList: ["业主", "家属", "租客"],
    timestart:0,
    timeend:0

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var _that = this;

    if (_that.data.id != 0){
      var url = app.globalData.domain + "/client/user/getInviteUserDes";
      var parm = {
        token: wx.getStorageSync("token"),
        id: _that.data.id
      }
      wxRequest.postRequest(url, parm).then(res => {
        console.log("家属详情", res);
        _that.setData({
          familyInfo: res.data
        });
      }, error => {
        Common.showToast(error);
      });
    }else{
      if (wx.getStorageSync("invireFamilyInfo")){
        _that.setData({
          familyInfo: wx.getStorageSync("invireFamilyInfo")
        });
      }
      
    }

    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //点击开始的时间  
  timestart: function (e) {
    var _this = this;
    _this.setData({ timestart: e.timeStamp });
  },
  //点击结束的时间
  timeend: function (e) {
    var _this = this;
    _this.setData({ timeend: e.timeStamp });
  },

  // 保存图片方法
  saveImage(filePath) {
    let self = this
    console.log(filePath);
    wx.getImageInfo({
      src: filePath,
      success: function (sres) {
        console.log(sres.path);
        console.log(sres);
        wx.saveImageToPhotosAlbum({
          filePath: sres.path,  // 此为图片路径
          success: (res) => {
            console.log(res)
            Common.showSuccessToast('保存成功')
          },
          fail: (err) => {
            console.log(err)
            Common.showToast('保存失败，请稍后重试')
          }
        })
      }

    })
    
  },
  // 点击保存图片到相册(授权)
  saveImageToPhotos(event) {
    console.log("event",event);
    var filePath = event.currentTarget.dataset.src;
    let self = this
    var times = self.data.timeend - self.data.timestart;
    if (times > 300) {
      // 相册授权
      wx.getSetting({
        success(res) {
          // 进行授权检测，未授权则进行弹层授权
          if (!res.authSetting['scope.writePhotosAlbum']) {
            wx.authorize({
              scope: 'scope.writePhotosAlbum',
              success() {
                self.saveImage(filePath)
              },
              // 拒绝授权时，则进入手机设置页面，可进行授权设置
              fail(res) {
                console.log(res);
                if (res.errMsg === "authorize:fail auth deny") {
                  Common.showToast("前往设置，打开相册授权，才正常使用");
                }
              }
            })
          } else {
            // 已授权则直接进行保存图片
            self.saveImage(filePath)
          }
        },
        fail(res) {
          console.log(res);
        }
      })
    }
  },

  //删除家属
  delInviteUser:function(){
    var _that = this;

    wx.showModal({
      title: '提示',
      content: '是否确定删除该用户，删除后此用户将无法接收授权房屋的信息',
      success(res) {
        if (res.confirm) {
          if (_that.data.id != 0) {
            var url = app.globalData.domain + "/client/user/delInviteUser";
            var parm = {
              token: wx.getStorageSync("token"),
              id: _that.data.id
            }
            wxRequest.postRequest(url, parm).then(res => {
              Common.showSuccessToast("删除成功");
              setTimeout(function () {
                wx.navigateBack()
              }, 1500);
            }, error => {
              Common.showToast(error);
            });
          } else {
            Common.showToast("信息异常");
          }
          
        }
      }
    })


    

    








  }



})